﻿using System;
using System.Collections.Generic;

using LCL.CodeHelper;
using LCL.Config;
using LCL.LData;

namespace LCL.CodeEngine
{
    [Serializable]
    public class DatabaseHost : TemplateHost
    {
        private string _dbname;
        private List<TableModel> _tablelist;
        private string _dbtype;
        private string _dbhelperName = "Utils.GetFactory()";
        private DbConfigInfo _dbset;
        private string _format = "uc_|lcl_|table|zt_|lmg|zte|cms_|tky_|base_";
        public DbConfigInfo DbSet
        {
            get
            {
                return this._dbset;
            }
            set
            {
                this._dbset = value;
            }
        }
        public string Format
        {
            get
            {
                return this._format;
            }
            set
            {
                this._format = value;
            }
        }
        public string DbName
        {
            get
            {
                return this._dbname;
            }
            set
            {
                this._dbname = value;
            }
        }
        public List<TableModel> TableList
        {
            get
            {
                return this._tablelist;
            }
            set
            {
                this._tablelist = value;
            }
        }
        public string DbType
        {
            get
            {
                return this._dbtype;
            }
            set
            {
                this._dbtype = value;
            }
        }
        public string DbHelperName
        {
            get
            {
                return this._dbhelperName;
            }
            set
            {
                this._dbhelperName = value;
            }
        }
        public string DbParaHead
        {
            get
            {
                return CodeCommon.DbParaHead(this.DbType);
            }
        }
        public string DbParaDbType
        {
            get
            {
                return CodeCommon.DbParaDbType(this.DbType);
            }
        }
        /// <summary>
        /// 当前数据库类型对应的存储过程参数符合,例如：@
        /// </summary>
        public string preParameter
        {
            get
            {
                return CodeCommon.preParameter(this.DbType);
            }
        }
        /// <summary>
        /// 第一个字母大写
        /// </summary>
        /// <param name="dbTableName"></param>
        /// <returns></returns>
        public string GetClassName(string dbTableName)
        {
            return CodeCommon.GetClassName(dbTableName, Format);
        }
        public string GetClassName(string dbTableName, bool flg)
        {
            if (flg)
            {
                return NameRule.CreateLower(Format).GetClassName(dbTableName);
            }
            else
            {
                return NameRule.CreateUpperr(Format).GetClassName(dbTableName);
            }
        }
        public string GetModelClass(string TabName) {
            return GetClassName(TabName);
        }
        public string GetBLLClass(string TabName) {
            return GetClassName(TabName);
        }
        public string GetDALClass(string TabName) {
            return GetClassName(TabName);
        }
        public int CompareByintOrder(ColumnInfo x,ColumnInfo y) {
            if(x == null) {
                if(y == null) {
                    return 0;
                }
                return -1;
            } else {
                if(y == null) {
                    return 1;
                }
                int num = 0;
                int num2 = 0;
                try {
                    num = Convert.ToInt32(x.ColumnOrder);
                } catch {
                    int result = -1;
                    return result;
                }
                try {
                    num2 = Convert.ToInt32(y.ColumnOrder);
                } catch {
                    int result = 1;
                    return result;
                }
                if(num < num2) {
                    return -1;
                }
                if(x == y) {
                    return 0;
                }
                return 1;
            }
        }
        public List<ColumnInfo> GetTablesColumnAll()
        {
            IDbObject iDbObject = DBOMaker.GetDbObject(DbConfig.GetConfig());
            List<ColumnInfo> list = new List<ColumnInfo>();
            foreach (var table in TableList)
            {
                list.AddRange(iDbObject.GetColumnInfoList(DbName, table.TableName));
            }
            return list;
        }
        public string Configure(string TabName, string format)
        {
            if (string.IsNullOrWhiteSpace(TabName) || string.IsNullOrWhiteSpace(format)) return "";
            string[] arr = format.Split('|');
            for (int i = 0; i < arr.Length; i++)
            {
                TabName = TabName.Replace(arr[i], "");
            }
            return TabName;
        }
        public string GenerateRandomCode(int length = 5) {
            return CodeCommon.GenerateRandomCode(length);
        }
    }
}
